package com.zigong.app.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.zigong.app.R;
import com.zigong.app.base.BaseActivity;
import com.zigong.app.beans.NewsItem;
import com.zigong.app.ui.utils.GlideUtil;
import com.zigong.app.ui.utils.SkipUtil;
import com.zigong.app.ui.widget.BasePlayerView;

import java.util.ArrayList;
import java.util.HashMap;

public class PlaceholderFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    private LinearLayout view;
    private BasePlayerView videoPlayer;
    private CustomFontTextView titleView;
    private Handler handler;

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
//        Bundle args = new Bundle();
//        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
//        fragment.setArguments(args);
        return fragment;
    }

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_video, container, false);
        videoPlayer = (BasePlayerView) rootView.findViewById(R.id.video_player);
        titleView = (CustomFontTextView) rootView.findViewById(R.id.video_title);
        return rootView;
    }


    public LinearLayout getLayoutView() {
        return view;
    }

    public BasePlayerView getVideoPlayer() {
        return videoPlayer;
    }

    public void setVideoPlayerPause() {
        if (null != videoPlayer) {
            videoPlayer.onVideoPause();
        }
    }

    @Override
    public void onPause() {
        if (videoPlayer != null) {
            videoPlayer.onVideoPause();
        }
        super.onPause();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (videoPlayer != null) {
            videoPlayer.onVideoPause();
        }
    }

    public void setVideoPlayerData(final Activity ac, final NewsItem item) {
        titleView.setText(item.getNewsTitle());
        titleView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = item.getNewsTitle();
                ArrayList<Pair<String, String>> pairs = new ArrayList<>();
                pairs.add(new Pair<>("页面", "首页"));
                pairs.add(new Pair<>("栏目", "精选"));
                pairs.add(new Pair<>("位置", "热门视频"));
                ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
                tempPair.addAll(pairs);
                tempPair.add(new Pair<>("行为类型", "点击"));
//            pairs.add(new Pair<>("行为类型", "点击"));

                GridsumWebDissector.getInstance().trackEvent(ac, "", title, "", 200, tempPair);

                HashMap<String, String> map = new HashMap<>();
                for (Pair<String, String> p : pairs) {
                    map.put(p.first, p.second);
                }
                SkipUtil.skipWithNewsItem(ac, item, map);
            }
        });

        handler = new Handler();
        if (null != videoPlayer) {
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (videoPlayer != null && videoPlayer.getThumbImge() != null) {
                        GlideUtil.getGlideWithLarge169DefNotFit(getContext(), item.getNewsImage()).into(videoPlayer.getThumbImge());
                    }
                    if (!TextUtils.isEmpty(item.getNewsMediaUrl())) {
                        videoPlayer.setUp(item.getNewsMediaUrl(), false, "");
                        videoPlayer.startPlay();
                    }
                }
            }, 200);
        }
    }
}
